<template>
	<main-container>
		<!-- 搜索 -->
		<el-form :model="state.queryParams" ref="queryRef" :inline="true" v-show="showSearch">
			<el-form-item label="物流名称">
				<el-input v-model="state.queryParams.name" clearable></el-input>
			</el-form-item>
			<el-form-item label="物流编码">
				<el-input v-model="state.queryParams.code" clearable></el-input>
			</el-form-item>
			<el-form-item>
				<el-button type="primary" @click="initPage">
					<el-icon> <ele-Search /> </el-icon>搜索</el-button
				>
				<el-button @click="resetQuery">
					<el-icon> <ele-Refresh /> </el-icon>重置</el-button
				>
			</el-form-item>
		</el-form>
		<Form ref="refForm" @initPage="initPage"></Form>
		<!-- 工具栏 -->
		<div class="hx-table-toolbar">
			<div>
				<el-button v-auth="'mall:logisticscompany:add'" type="primary" @click="add"
					><el-icon> <ele-Plus /> </el-icon>新增</el-button
				>
			</div>
			<right-toolbar :searchBtn="true" :refreshBtn="true" @search="showSearch = !showSearch" @refresh="initPage"></right-toolbar>
		</div>
		<el-table :data="state.tableData" v-loading="state.loading" border>
			<el-table-column prop="name" label="物流名称" />
			<el-table-column prop="code" label="物流编码" />
			<el-table-column prop="status" label="状态">
				<template #default="scope">
					<dict-tag :options="status" :value="scope.row.status"></dict-tag>
				</template>
			</el-table-column>
			<el-table-column label="操作" width="200" align="center">
				<template #default="scope">
					<el-button v-auth="'mall:logisticscompany:edit'" link type="primary" @click="edit(scope.row)"
						><el-icon> <ele-Edit /> </el-icon>修改</el-button
					>
					<el-button v-auth="'mall:logisticscompany:del'" link type="danger" @click="del(scope.row.id)"
						><el-icon> <ele-Delete /> </el-icon>删除</el-button
					>
				</template>
			</el-table-column>
		</el-table>
		<!-- 分页 -->
		<pagination
			v-model:total="state.page.total"
			v-model:currentPage="state.page.currentPage"
			v-model:pageSize="state.page.pageSize"
			@changePage="initPage"
		></pagination>
	</main-container>
</template>
<script lang="ts" setup>
import { defineAsyncComponent, ref, reactive } from 'vue';
import { getPage, delObj } from '/@/api/shop/logisticscompany';
import { ElMessageBox, ElMessage } from 'element-plus';
import { useDict } from '/@/utils/dict';
const Form = defineAsyncComponent(() => import('./form.vue'));
// 字典
const { status } = useDict('status');
const state = reactive({
	queryParams: { name: '', code: '' },
	page: {
		total: 0,
		currentPage: 1,
		pageSize: 10,
		ascs: [],
		descs: 'create_time',
	},
	tableData: [],
	rules: {
		type: [
			{
				required: true,
				message: 'Please select Activity zone',
				trigger: 'change',
			},
		],
	},
	loading: false,
});
const showSearch = ref(true);
const refForm = ref();
const initPage = async () => {
	state.loading = true;
	let params = {
		current: state.page.currentPage,
		size: state.page.pageSize,
		ascs: state.page.ascs,
		descs: state.page.descs,
	};
	await getPage(Object.assign(params, state.queryParams))
		.then((response: any) => {
			state.tableData = response.data.data.records;
			state.page.total = response.data.data.total;
			state.loading = false;
		})
		.catch(() => {
			state.loading = false;
		});
};
/**
 * 新增按钮
 */
const add = () => {
	refForm.value.initForm();
};
/**
 * 修改按钮
 */
const edit = (row: any) => {
	refForm.value.initForm(row);
};
/**
 * 删除按钮
 */
const del = (id: string) => {
	ElMessageBox.confirm('此操作将删除该物流公司，是否继续?', '提示', {
		confirmButtonText: '确认',
		cancelButtonText: '取消',
		type: 'warning',
	}).then(() => {
		delObj(id).then(() => {
			ElMessage.success('删除成功');
			initPage();
		});
	});
};
/**
 * 重置搜索表单
 */
const resetQuery = () => {
	state.queryParams = { name: '', code: '' };
};
initPage();
</script>
<style lang="scss" scoped></style>
